﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin.Design
{

    /// <summary>
    ///  提供一个用户界面，该界面可以在设计时编辑大多数类型的集合。
    /// </summary>
    public class CollectionEditor : System.ComponentModel.Design.CollectionEditor
    {
        PropertyGrid grid;

        /// <summary>
        /// 使用指定的集合类型初始化 System.ComponentModel.Design.CollectionEditor 类的新实例。
        /// </summary>
        /// <param name="type">此编辑器要编辑的集合的类型。</param>
        public CollectionEditor(Type type) : base(type)
        {
        }

        /// <summary>
        /// 创建新的窗体，以显示和编辑当前集合。
        /// </summary>
        /// <returns>作为用于编辑集合的用户界面提供的 System.ComponentModel.Design.CollectionEditor.CollectionForm。</returns>
        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm frm = base.CreateCollectionForm();
            FieldInfo fileinfo = frm.GetType().GetField("propertyBrowser", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fileinfo != null)
            {
                grid = fileinfo.GetValue(frm) as System.Windows.Forms.PropertyGrid;
                grid.HelpVisible = true;

                ContextMenuStrip mContextMenuStrip = new ContextMenuStrip();
                mContextMenuStrip.Items.Add("重置");
                mContextMenuStrip.Items[0].Click += new EventHandler(CustomCollectionEditor_Click);
                grid.ContextMenuStrip = mContextMenuStrip;
            }
            //frm.Width = 700;
            //frm.Height = 600;
            return frm;
        }

        private void CustomCollectionEditor_Click(object sender, EventArgs e)
        {
            grid.ResetSelectedProperty();
        }
    }
}
